import java.util.*;

public class LRUCacheDemo3<K, V> {

    //1.构造一个node节点作为数据载体
    class Node<K, V> {
        K key;
        V value;
        Node<K, V> prev, next;

        public Node() {
            this.prev = this.next = null;
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            this.prev = this.next = null;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node<?, ?> node = (Node<?, ?>) o;
            if (key == null || node.key == null) return false;
            return Objects.equals(key.toString(), node.key.toString());
        }

        @Override
        public int hashCode() {
            return Objects.hash(key);
        }
    }

    //2 构建一个虚拟的双向链表,,里面安放的就是我们的Node
    class DoubleLinkedList<K, V> {
        // 首尾虚拟节点不存储数据
        Node<K, V> head, tail;

        public DoubleLinkedList() {
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.prev = head;
        }

        //3. 添加到头
        public void addHead(Node<K, V> node) {
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;
        }

        //4.删除节点
        public void removeNode(Node<K, V> node) {
            node.next.prev = node.prev;
            node.prev.next = node.next;
            node.prev = null;
            node.next = null;
        }

        //5.获得最后一个节点
        public Node<K, V> getLast() {
            return tail.prev;
        }

        //6. 获取指定 key 的 node
        public Node<K, V> get (K key) {
            Node<K, V> node = head.next;
            while (!node.equals(tail) && !node.key.toString().equals(key.toString()))
                node = node.next;
            if (node.equals(tail)) return null;
            return node;
        }

        public void printTail2Head () {
            Node<K, V> node = tail.prev;
            System.out.printf("\033[32m tail ===>> \033[0m");
            while (! node.equals(head)) {
                System.out.printf("\033[32m key-%s => \033[0m", node.key);
                node = node.prev;
            }
            System.out.printf("\033[32m ===>> head \033[0m");
            System.out.println();
        }

    }

    private int size; // 链表的长度
    private int cacheSize; // 链表的长度阈值
    private DoubleLinkedList<K, V> doubleLinkedList;

    public LRUCacheDemo3(int cacheSize) {
        this.cacheSize = cacheSize; //坑位
        this.doubleLinkedList = new DoubleLinkedList<>();
    }

    // get 和 put 都是对 key 对应的 node 的使用
    public V get(K key) {
        Node<K, V> node = doubleLinkedList.get(key);
        if (! node.prev.equals(doubleLinkedList.head)) {
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        }

        return node.value;
    }

    public void put(K key, V value) {
        Node<K, V> node = doubleLinkedList.get(key);
        if (node != null) { //update
            if (node.prev.equals(doubleLinkedList.head)) return;
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        } else {
            if (size == cacheSize) { //坑位满了
                Node<K, V> lastNode = doubleLinkedList.getLast();
                doubleLinkedList.removeNode(lastNode);
                size--;
            }
            node = new Node<>(key, value);
            doubleLinkedList.addHead(node);
            size++;
        }
    }

    public void printTail2Head () {
        doubleLinkedList.printTail2Head();
    }

    public static void main(String[] args) {

        LRUCacheDemo3 lruCacheDemo = new LRUCacheDemo3(3);

        lruCacheDemo.put(1, 1);
        lruCacheDemo.put(2, 2);
        lruCacheDemo.put(3, 3);
        lruCacheDemo.get(1);
        lruCacheDemo.printTail2Head();

        lruCacheDemo.put(4, 1);
        lruCacheDemo.printTail2Head();

        lruCacheDemo.put(3, 1);
        lruCacheDemo.printTail2Head();
        lruCacheDemo.put(3, 1);
        lruCacheDemo.printTail2Head();
        lruCacheDemo.put(3, 1);
        lruCacheDemo.printTail2Head();
        lruCacheDemo.put(5, 1);
        lruCacheDemo.printTail2Head();

    }
}
